﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using StudentManage.Common;
using StudentManage.Data;

namespace StudentManage.Web.Admin.Management.SysManage
{
    public enum PrivType
    {
        Page = 0, Control = 1, Category = 2
    }

    public partial class PrivilegesList : SecurityPage
    {
        public override string PrivCode()
        {
            return "ec0e95d00d9e9f0b";
        }

        int i = 0;
        public string GetXN()
        {
            if (i == 0)
                i = (this.AspNetPager1.PageSize) * (this.AspNetPager1.CurrentPageIndex - 1);

            i += 1;
            return i.ToString();
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            ddlModuleType.AutoPostBack = true;
            ddlModuleType.SelectedIndexChanged += new EventHandler(ddlModuleType_SelectedIndexChanged);
            if (IsPostBack) return;
            BindData();
        }

        protected void RepList_ItemCommand(object source, RepeaterCommandEventArgs e)
        {
            if (e.CommandName == "Del")
            {
                Privileges.Delete("PrivCode=@0", e.CommandArgument.ToString());
                BindData();
            }
        }

        public void BindData()
        {
            List<Privileges> list = LinqUtility.LinqContext.Privileges.Where(p=>p.PrivName.Contains(this.txt_SearchPrivName.Text.Trim())).OrderBy(p => p.PrivType).ToList();
            this.RepList.DataSource = list;
            this.RepList.DataBind();

            ddlParentNode.Items.Clear();
            ddlParentNode.Items.Add(new ListItem() { Text = "根节点", Value = "Root" });
            ddlParentNode.Items.AddRange(LinqUtility.LinqContext.Privileges.Where(p => p.PrivType == PrivType.Category.EToString())
                .Select(a => new ListItem() { Text = a.PrivName, Value = a.PrivCode }).ToArray());

            ddlPageNode.Items.Clear();
            ddlPageNode.Items.AddRange(LinqUtility.LinqContext.Privileges.Where(p => p.PrivType == PrivType.Page.EToString())
                .Select(a => new ListItem() { Text = a.PrivName, Value = a.PrivCode }).ToArray());
        }

        public string LoadModuleType(string p)
        {
            if (p.IsNullOrEmpty()) return string.Empty;
            try
            {
                switch (p)
                {
                    case "Page":
                        return "页面";
                    case "Control":
                        return "控件";
                    case "Category":
                        return "分类";
                }
            }
            catch (Exception)
            {

            }
            return "";
        }

        public void delete_Click(object sender, EventArgs e)
        {
            Button delete = sender as Button;
            string privCode = delete.CommandArgument;
            Privileges.Delete("PrivCode==@0", privCode);
            BindData();
        }

        void ddlModuleType_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (ddlModuleType.SelectedValue == "0")
            {
                PrivModuleView.Visible = false;
                SysModuleView.Visible = true;
            }
            else
            {
                PrivModuleView.Visible = true;
                SysModuleView.Visible = false;
            }
        }

        protected void btSave_Click(object sender, EventArgs e)
        {
            PrivType ParentPrivType = ddlParentNode.SelectedValue == "Root" ? PrivType.Category : PrivType.Page;
            string ParentCode = string.Empty, ParentName = string.Empty;

            if (ddlModuleType.SelectedValue == "1")
            {
                ParentPrivType = PrivType.Control;
                ParentCode = ddlPageNode.SelectedItem.Value;
                ParentName = ddlPageNode.SelectedItem.Text;
            }
            else
            {
                ParentCode = ddlParentNode.SelectedItem.Value;
                ParentName = ddlParentNode.SelectedItem.Text;
            }
            string PrivCode = Utility.NewPrivCode();
            if (!txtPrivCode.Text.IsNullOrEmpty())
            {
                PrivCode = txtPrivCode.Text;
            }

            Privileges newPriv = new Privileges()
            {
                PrivCode = PrivCode,
                PrivName = txtSysModuleName.Text.Trim(),
                PrivDesc = txtSysModuleDesc.Text.Trim(),
                PrivType = ParentPrivType.EToString(),

                PrivParm1 = ParentName,
                ParentPrivCode = ParentCode
            };
            Privileges.Insert(newPriv);

            BindData();
        }

        protected void Btn_Search_Click(object sender, EventArgs e)
        {
            this.BindData();
        }

        protected void GridView1_PreRender(object sender, EventArgs e)
        {
            //if (GridView1.Rows.Count > 0)
            //{
            //    // 使用<TH>替换<TD>
            //    GridView1.UseAccessibleHeader = true;

            //    //This will add the <thead> and <tbody> elements
            //    //HeaderRow将被<thead>包裹，数据行将被<tbody>包裹
            //    GridView1.HeaderRow.TableSection = TableRowSection.TableHeader;

            //    // FooterRow将被<tfoot>包裹
            //    GridView1.FooterRow.TableSection = TableRowSection.TableFooter;
            //}
        }
    }
}